"""
#===============================================================================
# License
#===============================================================================
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

#===============================================================================
Copyright (c) 2009 bryan p. conrad
bconrad2@gmail.com

Repository Path:    
$HeadURL$

Created on:           Aug 25, 2009
Last Revised on:      $Date$
Version:              $Revision$
ID:                   $Id$
Author:               $Author$ :: (bryan)

#===============================================================================


#===============================================================================
# Documentation
#===============================================================================

"""

#===============================================================================
# Imports
#===============================================================================

import JT
import time
import numpy as np


def test(vert,proj,body):
    Data = JT.Data.gold_standard()    
    #===============================================================================
    #    Setup DRR
    #===============================================================================      
    drr = JT.DRR()
    drr.SetExternalCalibration(Data.Frames[vert].FixedImages[proj].calibration)
    
    drr.AddVolume(Data.Frames[vert].Volumes[0].data)
    drr.AddBodyCenter(Data.Frames[vert].Volumes[0].BodyCenters[body])
    drr._renWin.SetOffScreenRendering(0)
    
    reg=JT.Registration()
    reg.SetFixedImage(Data.Frames[vert].FixedImages[proj].data.GetImage(0))
    reg.SetMovingImageGenerator(drr)
    reg.SetRegionOfInterest(Data.Frames[vert].FixedImages[proj].ROIs[body].data)
    
    #===============================================================================
    #    Setup Registration
    #===============================================================================      
    # set Metric
    reg.SetImageMetric('bpc')
    # Set Optimizer
    gd = JT.OptimumStepGradientDescentOptimizer()
    reg.SetOptimizer(gd)
    
    Testing = JT.Testing()
     # Set the starting pose
    pose = Testing.GetStartPose(100)
    print "Starting Pose: ", pose
    reg.SetInitialPose(pose)
    
    # Set Scales 1/1000 for translations, 1 for rotations
    scales = [1.0,1.0,1.0,1900.0,1900.0,1900.0]
    reg.SetScales(scales)
    
    # Registration Options
    reg._plot = False
    reg._debug = True
    reg._logGradientResult = False
    gd._debug = True
    
    #===============================================================================
    # Setup Optimizer Parameters
    #===============================================================================
    # Set the cost function
    gd.SetCostFunction(reg)
    
    # Modify Optimizer Parameters
    gd.SetMaximumStepLength(10)
    gd.SetMaximumGradientStepLength(1.1)
    gd.SetMinimumGradientStepLength(0.9)
    gd.SetMinimumStepLength(.01)
    gd.SetRelaxationFactor(0.85)
    gd.SetNumberOfIterations(50)
    gd._MinimumGradientStepLength = 0.25
    
    reg.Start()
    # Print Final State    
    gd.PrintSelf() 


if __name__ == '__main__':
    # Define which data to use for registration:
    vert = 0
    proj = 1
    body = 1
    
    test(vert,proj,body)
